//
//  GetLoginUserVm.swift
//  Pods
//
//  Created by eme on 2017/3/28.
//
//
import SwiftyJSON
import RxSwift
import Result
import NetWorkCore
import ModelProtocol
import SwiftyUserDefaults
import BrandonCore

class GetLoginUserVm{
    
    let getLoginUser = PublishSubject<Void>()
    
    let backData = PublishSubject<UserModel>()
    
    let error = PublishSubject<MikerError>()
    
    let disposeBag = DisposeBag()
    
    init(){
        
        let skuRequest = getLoginUser.map{ _ in
            
            return MineApi.getLoginUserModel()
            
            }.shareReplay(1)
        
        let skuResponse = skuRequest.emeRequestApiForObj(UserModel.self).shareReplay(1)
        
        skuResponse.subscribe(onNext:{ [unowned self] result in
            switch result{
            case .success(let data):
                
                AppData.updateUserModel(data)
                self.backData.onNext(data)
                
            case .failure(let error):
                
                self.error.onNext(error)
                
            }
        }).addDisposableTo(disposeBag)
        
    }
}
class LoginVm{
    
    let getLoginUser = PublishSubject<String>()
    
    let backData = PublishSubject<UserModel>()
    
    let error = PublishSubject<MikerError>()
    
    let disposeBag = DisposeBag()
    
    init(){
        
        let skuRequest = getLoginUser.map{ _ -> MineApi in
            
            let devid = NSUUID().uuidString
            Defaults[.uuid] = devid
//            AppData.sharedInstance.devid = devid
            NetWorkAPI.sharedInstance.headers["devid"] = devid
            
            return MineApi.login(id: "18212345678", password: "qqq111")
            
            }.shareReplay(1)
        
        let skuResponse = skuRequest.emeRequestApiForObj(UserModel.self).shareReplay(1)
        
        skuResponse.subscribe(onNext:{ [unowned self] result in
            switch result{
            case .success(let data):
                
                
//                AppData.updateUserModel(user: data)
                self.backData.onNext(data)
                
            case .failure(let error):
                
                self.error.onNext(error)
                
            }
        }).addDisposableTo(disposeBag)
        
    }
}





